<template>
  <el-menu
      :default-active="activeIndex"
      class="el-menu"
      mode="horizontal"
      :ellipsis="false"
      text-color="#333"
      active-text-color="#409EFF"
      @select="handleSelect"
  >
    <div class="logo-container">
      <span class="logo">LearnShare</span>
    </div>
    <div class="flex-grow" />
    <el-menu-item index="0">
      <div class="menu-item-content">
        <img src="../assets/icon/首页.png" class="icon" />
        <router-link to="/">首页</router-link>
      </div>
    </el-menu-item>
    <el-menu-item index="1">
      <div class="menu-item-content">
        <img src="../assets/icon/个人介绍.png" class="icon" />
        <router-link to="/allBlog">精选文章</router-link>
      </div>
    </el-menu-item>
    <el-menu-item index="2">
      <div class="menu-item-content">
        <img src="../assets/icon/个人介绍.png" class="icon" />
        <router-link to="/deepseek">D一下</router-link>
      </div>
    </el-menu-item>
    <el-menu-item index="3">
      <div class="menu-item-content">
        <a href="http://47.108.29.116:40116" class="external-link">it-tools</a>
      </div>
    </el-menu-item>

    <div class="user-section">
      <router-link to="/addArticle">
        <el-button type="primary" class="create-btn">创作中心</el-button>
      </router-link>
      <el-dropdown>
        <div class="user" v-if="isLoggedIn">
          <el-avatar :src="user_pic" :size="32" class="user-pic" />
          <span class="username">{{ username }}</span>
        </div>
        <el-button class="login-btn" v-else @click="login">登录/注册</el-button>
        <template #dropdown>
          <el-dropdown-menu v-if="isLoggedIn">
            <el-dropdown-item @click="myInfo">
              <i class="el-icon-user"></i> 个人信息
            </el-dropdown-item>
            <el-dropdown-item @click="application">
              <i class="el-icon-user"></i> 审核进度
            </el-dropdown-item>
            <el-dropdown-item @click="logout">
              <i class="el-icon-switch-button"></i> 退出登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </el-menu>
</template>

<script lang="ts" setup>
import { ref } from 'vue';

const activeIndex = ref('1');
const handleSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
};

const isLoggedIn = ref(false); // 使用 ref 创建响应式变量
const user_pic = ref('');
const username = ref('');

// 解析 token
import { getToken, parseToken } from '@/api/tokenDecode';
import router from "@/router";
import request from "@/api/request";
import {ElMessage} from "element-plus";

const addArticle = () => {
  router.push({ name: 'AddArticle' });
}
const myInfo = () => {
  router.push({ name: 'MyInfo' });
}
const application = () => {
  router.push({ name: 'application' });
}
const getUsernameFromToken = () => {
  try {
    const token = getToken(); // 获取 token
    console.log(token)
    // 验证 token 是否为空或有效
    if (!token) {
      console.error('无效的 Token');
      return { error: '无效的 Token' };
    }

    const decodedToken = parseToken(token); // 解析 token
    if (!decodedToken || !decodedToken.claims) {
      localStorage.clear();
      console.error('没有有效的 Token 或 claims 不存在');
      return { error: '没有有效的 Token 或 claims 不存在' };
    }

    const username = decodedToken.claims.username;
    const user_pic = decodedToken.claims.user_pic;

    if (!username) {
      localStorage.clear();
      console.error('没有有效的 Token 或 username 不存在');
      return { error: '没有有效的 Token 或 username 不存在' };
    }

    return { username, user_pic }; // 返回包含 username 和 userPic 的对象
  } catch (error) {
    localStorage.clear();
    console.error('获取用户名时发生错误:', error);
    return { error: '获取用户名时发生错误' };
  }
};


// 判断用户名
const userInfo = getUsernameFromToken();
if (userInfo) {
  isLoggedIn.value = true; // 更新响应式变量
  username.value = userInfo.username;
  user_pic.value = userInfo.user_pic;
} else {

  console.log('无法获取用户名');
}

const logout =  () => {
  localStorage.removeItem('token')
  localStorage.removeItem('tokenStartTime')
  localStorage.removeItem('userInfo')
  ElMessage.success('登出成功')
  setTimeout(function () {
    router.push('/login');
  }, 1000)
/*    // 发送登出请求
    request.post(`/user/logout`).then(() => {
      // 清除本地存储的用户信息或令牌
      localStorage.clear(); // 清除所有本地存储数据
      // 跳转到登录页面
      ElMessage.success('登出成功')
      setTimeout(function () {
         router.push('/login');
      }, 1000)
    }).catch((error: any) => {
      console.error("登出失败:", error);
    });*/
};

const login = () => {
  router.push('/login');
};
</script>


<style scoped>
.el-menu {
  position: fixed;
  top: 0;
  width: 100%;
  height: 64px;
  background-color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  z-index: 1000;
  display: flex;
  align-items: center;
  padding: 0 24px;
  box-sizing: border-box;
  transition: all 0.3s ease;
}

.logo-container {
  margin-right: 40px;
  transition: all 0.3s ease;
}
.menu-item-content a {
  text-decoration: none !important;
}

.menu-item-content a:hover {
  text-decoration: none !important;
}
.logo {
  font-size: 26px;
  font-weight: bold;
  font-family: "华文行楷", cursive;
  background: linear-gradient(135deg, #409EFF 0%, #67C23A 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  letter-spacing: 1px;
}

.flex-grow {
  flex-grow: 1;
}

.el-menu-item {
  height: 64px;
  line-height: 64px;
  margin: 0 12px;
  border-bottom: 3px solid transparent;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  font-size: 15px;
  font-weight: 500;
}

.el-menu-item:hover {
  background-color: rgba(64, 158, 255, 0.05);
  border-bottom-color: rgba(64, 158, 255, 0.5);
}

.el-menu-item.is-active {
  border-bottom-color: #409EFF;
}

.menu-item-content {
  display: flex;
  align-items: center;
  height: 100%;
  padding: 0 8px;
}

.icon {
  width: 20px;
  height: 20px;
  margin-right: 8px;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.1));
}

.user-section {
  display: flex;
  align-items: center;
  margin-left: 24px;
  gap: 16px;
}

.user {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 20px;
  transition: all 0.3s ease;
}

.user:hover {
  background-color: rgba(64, 158, 255, 0.1);
}

.user-pic {
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.user-pic:hover {
  transform: scale(1.1);
}

.username {
  margin-left: 10px;
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.login-btn {
  color: #409EFF;
  border: 1px solid #409EFF;
  background-color: transparent;
  transition: all 0.3s ease;
  border-radius: 20px;
  padding: 8px 16px;
  font-weight: 500;
}

.login-btn:hover {
  color: #fff;
  background-color: #409EFF;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
}

.create-btn {
  margin-right: 16px;
  font-weight: 500;
  border-radius: 20px;
  padding: 10px 18px;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);
}

.external-link {
  color: inherit;
  text-decoration: none;
  transition: color 0.3s ease;
}

.external-link:hover {
  color: #409EFF;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .el-menu {
    padding: 0 16px;
    height: 56px;
  }

  .logo-container {
    margin-right: 20px;
  }

  .logo {
    font-size: 22px;
  }

  .el-menu-item {
    height: 56px;
    line-height: 56px;
    margin: 0 6px;
    font-size: 14px;
  }

  .user-section {
    margin-left: 12px;
    gap: 12px;
  }

  .create-btn {
    margin-right: 12px;
    padding: 8px 14px;
    font-size: 13px;
  }

  .login-btn {
    padding: 6px 12px;
    font-size: 13px;
  }
}
</style>
